namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using DotNetNuke.Entities.Modules.Definitions;
    using System;
    using System.IO;

    public class PaDnnLoaderFactory : ResourceInstallerBase
    {
        private PaInstallInfo _installerInfo;

        public PaDnnLoaderFactory(PaInstallInfo InstallerInfo)
        {
            this._installerInfo = InstallerInfo;
        }

        public PaDnnAdapterBase GetDnnAdapter()
        {
            ModuleDefinitionVersion Version = this.GetModuleVersion();
            PaDnnAdapterBase retValue = null;
            switch (((int) Version))
            {
                case 0:
                    throw new Exception(base.EXCEPTION_Format);

                case 1:
                    return retValue;

                case 2:
                    return new PaDnnAdapter_V2(this.InstallerInfo);

                case 3:
                    return new PaDnnAdapter_V2Skin(this.InstallerInfo);

                case 4:
                    return new PaDnnAdapter_V2Provider(this.InstallerInfo);

                case 5:
                    return new PaDnnAdapter_V3(this.InstallerInfo);
            }
            return retValue;
        }

        public PaDnnInstallerBase GetDnnInstaller()
        {
            ModuleDefinitionVersion Version = this.GetModuleVersion();
            PaDnnInstallerBase retValue = null;
            switch (((int) Version))
            {
                case 0:
                    throw new Exception(base.EXCEPTION_Format);

                case 1:
                    return retValue;

                case 2:
                    return new PaDnnInstallerBase(this.InstallerInfo);

                case 3:
                    return new PaDnnInstaller_V2Skin(this.InstallerInfo);

                case 4:
                    return new PaDnnInstaller_V2Provider(this.InstallerInfo);

                case 5:
                    return new PaDnnInstaller_V3(this.InstallerInfo);
            }
            return retValue;
        }

        private ModuleDefinitionVersion GetModuleVersion()
        {
            if (this.InstallerInfo.DnnFile != null)
            {
                MemoryStream buffer = new MemoryStream(this.InstallerInfo.DnnFile.Buffer, false);
                ModuleDefinitionValidator xval = new ModuleDefinitionValidator();
                return xval.GetModuleDefinitionVersion(buffer);
            }
            return ModuleDefinitionVersion.VUnknown;
        }

        public PaInstallInfo InstallerInfo
        {
            get
            {
                return this._installerInfo;
            }
        }
    }
}

